package com.ttn.common.core.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ttn.common.core.exception.BaseException;
import com.ttn.common.core.exception.ErrorType;
import com.ttn.common.core.exception.SystemErrorType;
import lombok.Getter;

import java.time.Instant;
import java.time.ZonedDateTime;

@Getter
public class Result<T> {

    public static final String SUCCESSFUL_CODE = "200";
    public static final String FAIL_CODE = "500";
    public static final String SUCCESSFUL_MESG = "处理成功";

    private String code;

    private boolean success;

    private String msg;

    private Instant timestamp;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result() {
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * @param errorType
     */
    public Result(ErrorType errorType) {
        this.code = errorType.getCode();
        this.msg = errorType.getMsg();
        this.success = false;
        this.timestamp = ZonedDateTime.now().toInstant();
        this.data = (T) "";
    }

    /**
     * @param errorType
     * @param data
     */
    public Result(ErrorType errorType, T data) {
        this(errorType);
        this.data = data;
    }

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param mesg
     * @param data
     */
    private Result(String code, String mesg, T data, boolean success) {
        this.code = code;
        this.msg = mesg;
        this.data = data;
        this.success = success;
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * @param success
     * @param mesg
     */
    public Result(boolean success, String mesg) {
        this.success = success;
        this.msg = mesg;
        this.timestamp = ZonedDateTime.now().toInstant();
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result success(Object data) {
        return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data, true);
    }

    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result success() {
        return success("");
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail() {
//        Result result = new Result(SystemErrorType.SYSTEM_ERROR_NEW);
        Result result = new Result();
        return result;
    }

    /**
     * 返回错误消息
     *
     * @return Result
     */
    public static Result fail(String msg) {
        return new Result<>(FAIL_CODE, msg, "", false);
    }

    /**
     * 返回错误消息 (异常处理)
     *
     * @return Result
     */
    public static Result fail(String code, String msg) {
        return new Result<>(code, msg, "", false);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @param baseException
     * @return Result
     */
    public static Result fail(BaseException baseException) {
        return fail(baseException, "");
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result fail(BaseException baseException, Object data) {
        return new Result<>(baseException.getErrorType(), data);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorType
     * @param data
     * @return Result
     */
    public static Result fail(ErrorType errorType, Object data) {
        return new Result<>(errorType, data);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorType
     * @return Result
     */
    public static Result fail(ErrorType errorType) {
        return Result.fail(errorType, "");
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result fail(Object data) {
//        return new Result<>(SystemErrorType.SYSTEM_ERROR_NEW, data);
        return null;
    }

    /**
     * 成功code=000000
     *
     * @return true/false
     */
//    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESSFUL_CODE.equals(this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
//    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result not_login() {
//        return new Result<>(SystemErrorType.USER_NOT_LOGIN.getCode(), SystemErrorType.USER_NOT_LOGIN.getMsg(), "",
//                true);
        return null;
    }
}
